﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace FileCluster
{
    public partial class Form1 : Form
    {
        protected string FolderPath = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void btSelectFolder_Click(object sender, EventArgs e)
        {
            if (FolderDlg.ShowDialog() == DialogResult.OK)
            {
                FolderPath = FolderDlg.SelectedPath;
                tbFolderPath.Text = FolderPath;
            }
        }

#region MultiThread
        
        private void btAnalyse_Click(object sender, EventArgs e)
        {
            btAnalyse.Enabled = false;
            OutputHandler = new OutputDelegate(DoOutput);
            CallBackHandler = new CallBackDelegate(DoCallBack);
            CallBackSilentHandler = new CallBackDelegateSilent(DoCallBack);
            ThreadStart workerStart = new ThreadStart(StartWork);
            _WorkThread = new Thread(workerStart);
            _WorkThread.Start();
        }

        public delegate int OutputDelegate(MessageContainer message);
        public OutputDelegate OutputHandler = null;

        public delegate void CallBackDelegate(MessageContainer message);
        public delegate void CallBackDelegateSilent();
        public CallBackDelegate CallBackHandler = null;
        public CallBackDelegateSilent CallBackSilentHandler = null;
        private Thread _WorkThread = null;

        public void StartWork()
        {
            try
            {
                Analyse analysor = new Analyse(FolderPath, this);
                analysor.Run();
            }
            catch (System.Threading.ThreadAbortException)
            {
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                StopWork();
            }
        }

        /// <summary>
        /// 停止工作。停止线程。
        /// </summary>
        public void StopWork()
        {
            if (_WorkThread != null)
            {
                if (_WorkThread.ThreadState == ThreadState.Background || _WorkThread.ThreadState == ThreadState.Running)
                {
                    _WorkThread.Abort();
                }
            }
        }

        /// <summary>
        /// 外部类通过此方法输出信息,接受MessageContainer封装好的Message输入。
        /// </summary>
        /// <param name="message"></param>
        public void Output(MessageContainer message)
        {
            this.Invoke(OutputHandler, new object[] { message });
        }

        int DoOutput(MessageContainer message)
        {
            StringBuilder sb = new StringBuilder(tbOutput.Text);
            sb.AppendLine(message.MessageContent);
            tbOutput.Text = sb.ToString();
            return message.MessageContent.Length;
        }

        /// <summary>
        /// 线程执行结束后的回调方法。线程结束前调用此方法通知主线程其结束。
        /// </summary>
        /// <param name="message">提供给主线程的信息</param>
        public void CallBack(MessageContainer message)
        {
            this.Invoke(CallBackHandler, new object[] { message });
        }

        /// <summary>
        /// 线程执行结束后的回调方法。线程结束前调用此方法通知主线程其结束。
        /// </summary>
        public void CallBack()
        {
            this.Invoke(CallBackSilentHandler);
        }

        void DoCallBack()
        {
            btAnalyse.Enabled = true;
        }

        void DoCallBack(MessageContainer message)
        {
            
        }
#endregion
        
    }

    public class MessageContainer
    {
        public string MessageContent { get; set; }
    }
}
